<?php

class Rootmaster extends CI_Controller{

	function __construct(){
		parent::__construct();
		$this->load->model('admin_model');
	}
	
	function index(){
		$this->load->view('admin/login');
	}
	
	function login(){
		$this->load->library('form_validation');
		//$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		$this->form_validation->set_rules('username', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');
		$this->form_validation->set_message('required','Kolom %s tidak boleh kosong');
		
		if($this->form_validation->run()==true){
			$username = $this ->input->post('usename');
			$password = md5($this->input->post('password'));
			$userData = $this->admin_model->cek_login($username,$password);
			if(!empty($userData)){
				$sessionData['id'] = $userData['id'];
				$sessionData['username'] = $userData['username'];
				$sessionData['nama_lengkap'] = $userData['nama_lengkap'];
				$sessionData['is_login'] = TRUE;
				
				$this->session->set_userdata($sessionData);
				
				redirect('admin/dashboard/index');
				//$this->load->view('admin/dashboard/index');
			}else{
				$this->session->set_flashdata('Login Failed!, email and password combination was wrong ');
				redirect('rootmaster/login');
			}
		}
		$this->load->view('admin/login');
	}
	
	function logout(){
		$this->session->sess_destroy();
		redirect('rootmaster/login');
	}
}